﻿using ctolua.Models.System;

namespace ctolua.Models
{
    /// <summary>
    /// 计时器
    /// </summary>
    public class MTimer
    {
        public Timer timer { get; set; }
        /// <summary>
        /// 计时器是否正在运行
        /// </summary>
        public bool? is_run => game_api.is_timer_valid(this.timer);

        /// <summary>
        /// 创建计时器
        /// </summary>
        /// <param name="delay ">  延迟   </param>
        /// <param name="count ">  执行次数   </param>
        /// <param name="immediately ">  立即运行   </param>
        /// <param name="callback ">  回调函数   </param>
        /// <returns>计时器</returns>
        public MTimer(double? delay, Action<MTimer> callback, int? count = -1, bool? immediately = false)
        {
            var data = new { };
            Action<dynamic> d_callback = (data) =>
            {
                callback(this);
            };
            this.timer = game_api.run_lua_timer(py.Fix32(delay), count, immediately, d_callback, data);
            SCache.setTimeCache(py.toString(this.timer), this);
        }

        public MTimer(Timer timer)
        {
            this.timer = timer;
        }

        /// <summary>
        /// 删除计时器
        /// </summary>
        public void del() => game_api.delete_timer(this.timer);

        /// <summary>
        /// 暂停计时器
        /// </summary>
        public void paus() => game_api.pause_timer(this.timer);

        ///恢复计时器
        public void resume() => game_api.resume_timer(this.timer);

        ///获取计时器设置的时间
        ///@return Float
        public double? get_time() => game_api.get_timer_time_out_time(this.timer).@float();

        ///获取计时器经过的时间
        ///@return Float
        public double? get_elapsed_time() => game_api.get_timer_elapsed_time(this.timer).@float();

        ///获取计时器剩余时间
        ///@return Float
        public double? get_remaining_time() => game_api.get_timer_remaining_time(this.timer).@float();

        ///获取计时器初始计数
        ///@return Int32
        public int? get_init_count() => game_api.get_timer_init_count(this.timer);

        ///获取计时器剩余计数
        ///@return Int32
        public int? get_remaining_count() => game_api.get_timer_remaining_count(this.timer);



        ///计时器是否存在
        ///@param timer Timer 计时器编号
        ///@return Bool
        public bool? is_null => game_api.timer_is_exist(this.timer);

        ///获取最近创建的计时器
        public MTimer get_last_created_timer() => new MTimer(game_api.get_last_created_timer());

        ///获取游戏开始时间戳
        ///@return Int32
        public static int? get_game_init_time_stamp() => game_api.get_game_init_time_stamp();

        ///获取本地时间戳
        ///@return Int32
        public static int? get_local_time_stamp() => game_api.get_local_time_stamp();
        ///获取独立计时器当前计时秒数
        ///@param name Str 倒计时名称
        ///@return Fixed
        public static double get_actor_timer_run_time(string name) => game_api.get_actor_timer_run_time(name).@float();

    }
}
